package com.taotao.manage.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.taotao.common.pojo.ItemCatData;
import com.taotao.common.pojo.ItemCatResult;
import com.taotao.common.service.RedisService;
import com.taotao.manage.pojo.ItemCat;

@Service
public class ItemCatService extends BaseService<ItemCat> {
	
	@Autowired
	private RedisService redisService;
	
	@Value("${TAOTAO_MANAGE_ITEM_CAT}")
	private String TAOTAO_MANAGE_ITEM_CAT;
	
	@Value("${TAOTAO_MANAGE_ITEM_CAT_TIMEOUT}")
	private Long TAOTAO_MANAGE_ITEM_CAT_TIMEOUT;

	public ItemCatResult queryItemCatResult() {
		
		// 到缓存中查询
		try {
			ItemCatResult itemCatResult = this.redisService.get(TAOTAO_MANAGE_ITEM_CAT, new TypeReference<ItemCatResult>() {
			});
			if(itemCatResult != null){
				return itemCatResult;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		ItemCatResult result = new ItemCatResult();
		
		// 获取所有的商品类目
		List<ItemCat> itemCatList = super.queryAll();
		
		// 处理所有类目
		Map<Long, List<ItemCat>> map = new HashMap<>();
		for (ItemCat itemCat : itemCatList) {
			if(!map.containsKey(itemCat.getParentId())){
				map.put(itemCat.getParentId(), new ArrayList<ItemCat>());
			}
			map.get(itemCat.getParentId()).add(itemCat);
		}
		
		// 获取一级类目
		List<ItemCat> lvl1ItemCatList = map.get(0l);
		// 创建一级类目的对象集合
		List<ItemCatData> lvl1ItemCatDataList = new ArrayList<>();
		for (ItemCat itemCat : lvl1ItemCatList) {
			if(lvl1ItemCatDataList.size() == 14){
				break;
			}
			// 创建一级类目对象
			ItemCatData lvl1ItemCatData = new ItemCatData();
			// 添加到集合对象中
			lvl1ItemCatDataList.add(lvl1ItemCatData);
			// 给一级类目对象设置参数
			lvl1ItemCatData.setUrl("/products/" + itemCat.getId() + ".html");
			lvl1ItemCatData.setName("<a href='/products/" + itemCat.getId() + ".html'>" + itemCat.getName() + "</a>");
			
			// 获取二级类目
			List<ItemCat> lvl2ItemCatList = map.get(itemCat.getId());
			// 创建二级类目对象的集合
			List<ItemCatData> lvl2ItemCatDataList = new ArrayList<>();
			for (ItemCat lvl2ItemCat : lvl2ItemCatList) {
				// 创建二级类目对象
				ItemCatData lvl2ItemCatData = new ItemCatData();
				// 把二级类目对象添加到二级类目对象集合中
				lvl2ItemCatDataList.add(lvl2ItemCatData);
				// 给二级对象设置参数
				lvl2ItemCatData.setUrl("/products/" + lvl2ItemCat.getId() + ".html");
				lvl2ItemCatData.setName(lvl2ItemCat.getName());
				
				// 获取三级类目
				List<ItemCat> lvl3ItemCatList = map.get(lvl2ItemCat.getId());
				// 创建三级类目的对象集合（String集合）
				List<String> lvl3List = new ArrayList<>();
				for (ItemCat lvl3ItemCat : lvl3ItemCatList) {
					lvl3List.add("/products/" + lvl3ItemCat.getId() + ".html|" + lvl3ItemCat.getName());
				}
				lvl2ItemCatData.setItem(lvl3List);
			}
			// 设置给一级类目对象
			lvl1ItemCatData.setItem(lvl2ItemCatDataList);
		}
		result.setItemCatDataList(lvl1ItemCatDataList);
		
		// 添加缓存
		try {
			this.redisService.set(TAOTAO_MANAGE_ITEM_CAT, result, TAOTAO_MANAGE_ITEM_CAT_TIMEOUT);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		
		return result;
	}

}
